package com.smrtcold.dev.plc;

import com.github.xingshuangs.iot.protocol.s7.enums.EPlcType;
import com.github.xingshuangs.iot.protocol.s7.service.S7PLC;
import com.smrtcold.dev.dto.dev.base.XMZDevInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Random;

public class S7Test1 {
   static S7PLC plc;
   private static Logger log = LoggerFactory.getLogger(S7Test1.class);

    public static void main(String[] args) throws Exception {
        S7Test1 test = new S7Test1();

        HashMap<String, XMZDevInfo> devconnect = new HashMap<>();//ConcurrentHashMap

        XMZDevInfo xmzDevInfo = new XMZDevInfo(EPlcType.S200_SMART, "192.168.2.100", 102, 1, "10000", "1000001", null);
        devconnect.put("1000010", xmzDevInfo);

        XMZDevInfo xmzDevInfo1 = new XMZDevInfo(EPlcType.S200_SMART, "192.168.2.100", 102, 1, "10000", "1000002", null);
        devconnect.put("1000002", xmzDevInfo1);


        System.err.println(devconnect.get("1000010").readAddres("VD 1038"));

        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Random random = new Random();
                while (true) {

                    try {
                        log.info("链接" +   xmzDevInfo.batch("VD", "VD1038", "VD1042", "VD1050", "VD1238", "VD1242", "VD1250"));
                    } catch (Exception e) {
                        System.err.println(e.getMessage());
                    }
                    try {
                        xmzDevInfo.writeByAddres("VD1038",random.nextDouble()*2);
                        xmzDevInfo.writeByAddres("VD1042",random.nextDouble()*4);
                        xmzDevInfo.writeByAddres("VD1050",random.nextDouble()*8);
                        xmzDevInfo.writeByAddres("VD1238",random.nextDouble()*10);
                        xmzDevInfo.writeByAddres("VD1242",random.nextDouble()*12);
                        xmzDevInfo.writeByAddres("VD1250",random.nextDouble()*15);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            }).start();

        }
        for (int i = 0; i < 100; i++) {
            new Thread(() -> {
                Random random = new Random();
                while (true) {
                    try {
                        xmzDevInfo.writeByAddres("VD1038",random.nextDouble()*20);
                        xmzDevInfo.writeByAddres("VD1042",random.nextDouble()*15);
                        xmzDevInfo.writeByAddres("VD1050",random.nextDouble()*12);
                        xmzDevInfo.writeByAddres("VD1238",random.nextDouble()*14);
                        xmzDevInfo.writeByAddres("VD1242",random.nextDouble()*10);
                        xmzDevInfo.writeByAddres("VD1250",random.nextDouble()*15);
                        log.info(":" + xmzDevInfo1.batch("VD", "VD1038", "VD1042", "VD1050", "VD1238", "VD1242", "VD1250"));
                    } catch (Exception e) {
                        e.printStackTrace();
                        System.err.println(e.getMessage());
                    }

                }
            }).start();
        }


//        short s = 6;
//
//        boolean b = plc.readBoolean("VB 227.2");
//
//        System.err.println(b);
//        plc.writeBoolean("VB 227.2", !b);
//        b = plc.readBoolean("VB 227.2");
//        System.err.println(b);
//
//        System.err.println("--------------------------");

//        plc.writeInt16("VW 2044", s);
//        //  plc.writeFloat32("VW 2044", 7);//ok不需要倍率
//        System.err.println(plc.readFloat64("VW 2044"));
//        System.err.println(plc.readFloat32("VW 2044"));
//        System.err.println(plc.readInt16("VW 2044"));
//        System.err.println(plc.readInt32("VW 2044"));
//        System.err.println(  plc.readBoolean("B 10.1","B 10.7"));

//        plc.writeInt16("VD 70",new Short("5"));
//        plc.writeInt32("VD 70",5);
//        plc.writeFloat32("VD 70",0.5f);/**/


//        List<Integer> integers = plc.readInt32("VD 80", "VD 274");
//        System.err.println(integers);
//        List<Float> Fs = plc.readFloat32("VD 80", "VD 274");
//        System.err.println(Fs);
//        List<Float> Fs1 = plc.readFloat32("VD 708", "VD 712");
//        System.err.println(Fs1);
//     while (true) {
//         List<Boolean> Fs1 = plc.readBoolean("VB 10.1", "VB 10.7");
//
//         System.err.println(Fs1);
//     }
        //vd
//        String addres = "VD 2016";
//        long l=System.currentTimeMillis();
//        for (int i = 0; i < 100; i++) {
//            System.err.println( plc.readFloat32(addres));
//        }
//        System.out.println("time:"+(System.currentTimeMillis()-l));
//
////         plc.writeInt32(addres,4488);
////        extracted(plc,addres);
//
////        String addres = "VW 880";
////        extracted(plc,addres);
////         plc.writeFloat32(addres,33.32f);
////        extracted(plc,addres);
//
//         addres = "VB 916";
//        extracted(plc,addres);
//     //   plc.writeBoolean(addres,false);
//        extracted(plc,addres);
        // plc.writeBoolean(addres,true);

    }

    private static void extracted(S7PLC plc, String addres) {
        System.err.println("read:" + addres + "=========================");
        try {
            System.err.println("int16:" + plc.readInt16(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("int32:" + plc.readInt32(addres));//
        } catch (Exception e) {
        }
        try {
            System.err.println("folt32:" + plc.readFloat32(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("folt64:" + plc.readFloat64(addres));
        } catch (Exception e) {
        }
        try {
            System.err.println("bool:" + plc.readBoolean(addres));
        } catch (Exception e) {

        }
        try {
            System.err.println("string:" + plc.readString(addres));
        } catch (Exception e) {

        }
    }


    public Number readAddres(String addres) {

        String _suf = addres.split(" ")[0];
        int i = _suf.lastIndexOf(".");
        if (i > 0) {
            _suf = _suf.substring(i + 1);
            addres = addres.replaceAll("." + _suf, "");
        }
        try {
            switch (_suf) {
                case "VD":
                    return this.plc.readFloat32(addres);//ReadUInt32  ReadInt16  ReadDouble}
                case "VW":
                case "LW":
                    return this.plc.readInt16(addres);
                case "M":
                case "B":
                case "Q":
                case "V":

                    return this.plc.readBoolean(addres) ? 1 : 0;

                case "SRW":
                    return null;
                default:
                    // log.warn("未找到：{}", point);
                    return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

}
